package com.community.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.community.dto.PurchaseOrderDto;
import com.community.dto.SupplierProductDto;
import com.community.pojo.*;
import com.community.service.IConsumerService;
import com.community.service.IProductService;
import com.community.service.ISupplierProductService;
import com.community.service.ISupplierService;
import com.community.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * <p>
 * 供货商商品关联表 前端控制器
 * </p>
 *
 * @author TangHaoSong Fly ChenJie HeYuHang
 * @since 2024-05-15
 */
@CrossOrigin
@Controller
@RequestMapping("/community/supplierProduct")
public class SupplierProductController {

    @Autowired
    private IProductService productService;

    @Autowired
    private ISupplierService supplierService;

    @Autowired
    private IConsumerService consumerService;

    @Autowired
    private ISupplierProductService supplierProductService;

    @RequestMapping("/purchaseById")
    @ResponseBody
    public List<SupplierProductDto> purchaseById(int productId){
        System.out.println(productId);
        List<SupplierProductDto> supplierList= this.supplierProductService.searchSupplier(productId);
        return supplierList;
    }

    @RequestMapping("/addProduct")
    @ResponseBody
    public List<PurchaseOrderDto> addProduct(int id){
        List<PurchaseOrderDto> list = this.supplierProductService.searchPurchase(id);
        return list;
    }
    @RequestMapping("/showById")
    @ResponseBody
    public Product showById(Integer productId) {
        Product byId = productService.getById(productId);
        return byId;
    }
    /**
     * 给商品绑定供应商 价格
     * @param supplierProduct
     * @return
     */
    @RequestMapping("/add")
    @ResponseBody
    public String add(@RequestBody SupplierProduct supplierProduct){
        System.out.print("supplierProduct++++"+supplierProduct);
        SupplierProduct supplierProduct1 = new SupplierProduct();
        supplierProduct1.setId(null);
        supplierProduct1.setPrice(supplierProduct.getPrice());
        //从供应商里面拿到供应商id
        QueryWrapper<Supplier> wrapper = new QueryWrapper<>();
        wrapper.eq("supplier_id",2);
        Supplier one = supplierService.getOne(wrapper);
        supplierProduct1.setSupplierId(one.getSupplierId());
        supplierProduct1.setProductId(supplierProduct.getProductId());
        //调用添加方法
        supplierProductService.save(supplierProduct1);
        return "ok";
    }
}
